//
//  AutoSizeLabelView.m
//  ZhenPin
//
//  Created by 董伟1 on 16/4/15.
//  Copyright © 2016年 zhenpin. All rights reserved.
//

#import "AutoSizeLabelView.h"

#define LabelDefaultTag         2000
#define LabelDefaultMarginX     12
#define LabelDefaultMarginY     15
#define LabelDefaultSpaceX      10
#define LabelDefaultSpaceY      10

@implementation AutoSizeLabelView

+ (CGFloat)sizeOfLabelView:(NSArray  *)array
{
    
    NSInteger  count = [array  count];
    CGFloat  originX = LabelDefaultMarginX;
    CGFloat  originY = LabelDefaultMarginY;
    
    CGFloat  width = SCREENWIDTH - 10;
    
    for (int i = 0; i < count; i++) {
        NSString  *titleStr = [[array  objectAtIndex:i] objectForKey:@"name"];
        UIFont  *font = [UIFont  systemFontOfSize:11.];
        //计算label的宽度
        CGSize  size = [titleStr sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 28.)];
        CGFloat  labWidth = size.width + 20;
        if (labWidth > width - LabelDefaultMarginX * 2) {
            labWidth = width - LabelDefaultMarginX * 2;
        }
        
        if (originX + labWidth > width - LabelDefaultMarginX) {
            //需要换行
            originX = LabelDefaultMarginX;
            originY = originY + 28. + LabelDefaultSpaceY;
        }
        
        //不需要换行
        originX = originX + labWidth + LabelDefaultSpaceX;
        
    }
    
    return originY + 28. + LabelDefaultMarginY;
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super  initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor  whiteColor];
    }
    
    return self;
}

- (void)reloadLabelViewWithArray:(NSArray  *)array
{
    for (UILabel  *tempLabel in self.subviews) {
        [tempLabel  removeFromSuperview];
    }
    
    NSInteger  count = [array  count];
    CGFloat  originX = LabelDefaultMarginX;
    CGFloat  originY = LabelDefaultMarginY;
    
//    UILabel  *lastLabel = nil;
    
    for (int i = 0; i < count; i++) {
        NSString  *titleStr = [[array  objectAtIndex:i] objectForKey:@"name"];
        UIFont  *font = [UIFont  systemFontOfSize:11.];
        //计算label的宽度
        CGSize  size = [titleStr sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 28.)];
        CGFloat  labWidth = size.width + 20;
        if (labWidth > self.frame.size.width - LabelDefaultMarginX * 2) {
            labWidth = self.frame.size.width - LabelDefaultMarginX * 2;
        }
        
        if (originX + labWidth > self.frame.size.width - LabelDefaultMarginX) {
            //需要换行
            originX = LabelDefaultMarginX;
            originY = originY + 28. + LabelDefaultSpaceY;
            //调整行最后一个label的宽度   让每行最后一个label离view的边距都是LabelDefaultMarginX
//            if (lastLabel) {
//                lastLabel.frame = CGRectMake(lastLabel.frame.origin.x, lastLabel.frame.origin.y, self.frame.size.width - LabelDefaultMarginX - lastLabel.frame.origin.x, lastLabel.frame.size.height);
//            }
        }
        
//        NSLog(@"========%f",originX);
        SelectLabel  *label = [[SelectLabel  alloc] initWithFrame:CGRectMake(originX, originY, labWidth, 28.)];
        label.text = titleStr;
        label.font = font;
        label.textColor = [UIColor  colorWithRed:153/255. green:153/255. blue:153/255. alpha:1.];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor  whiteColor];
        label.layer.cornerRadius = 14.;
        label.layer.masksToBounds = YES;
        label.layer.borderWidth = 0.5;
        label.layer.borderColor = [UIColor  colorWithRed:153/255. green:153/255. blue:153/255. alpha:1.].CGColor;
        label.tag = LabelDefaultTag + i;
        label.userInteractionEnabled = YES;
        label.selected = NO;
        
        UITapGestureRecognizer  *tap = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(singleTapAction:)];
        [label  addGestureRecognizer:tap];
        
        [self  addSubview:label];
        
        //不需要换行
        originX = originX + labWidth + LabelDefaultSpaceX;
        
//        lastLabel = label;
        
    }
    
    //修改自己的高度
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, originY + 28. + LabelDefaultMarginY);
}


- (void)singleTapAction:(UIGestureRecognizer *)gesture
{
    SelectLabel  *tempLab = (SelectLabel  *)[gesture  view];
    if (tempLab.selected) {
        tempLab.textColor = RGBACOLOR(153, 153, 153, 1.);
        tempLab.layer.borderColor = RGBACOLOR(153, 153, 153, 1.).CGColor;
        tempLab.selected = NO;
    }else {
        tempLab.textColor = RGBACOLOR(55, 164, 235, 1.);
        tempLab.layer.borderColor = RGBACOLOR(55, 164, 235, 1.).CGColor;
        tempLab.selected = YES;
    }
    
    if ([self.autoSizeLabDelegate  respondsToSelector:@selector(selectAutoSizeLabel:)]) {
        [self.autoSizeLabDelegate  selectAutoSizeLabel:tempLab.tag - LabelDefaultTag];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
